﻿using Lights.Processing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Lights.Controls
{
    /// <summary>
    /// Logica di interazione per ctrlHistogram.xaml
    /// </summary>
    public partial class HistogramViewer : UserControl
    {
        public static readonly DependencyProperty LuminanceProperty;
        public static readonly DependencyProperty RedProperty;
        public static readonly DependencyProperty BlueProperty;
        public static readonly DependencyProperty GreenProperty;

        public PointCollection Luminance
        {
            get { return (PointCollection)GetValue(HistogramViewer.LuminanceProperty); }
            set { SetValue(HistogramViewer.LuminanceProperty, value); }
        }

        public PointCollection Red
        {
            get { return (PointCollection)GetValue(HistogramViewer.RedProperty); }
            set { SetValue(HistogramViewer.RedProperty, value); }
        }

        public PointCollection Blue
        {
            get { return (PointCollection)GetValue(HistogramViewer.BlueProperty); }
            set { SetValue(HistogramViewer.BlueProperty, value); }
        }

        public PointCollection Green
        {
            get { return (PointCollection)GetValue(HistogramViewer.GreenProperty); }
            set { SetValue(HistogramViewer.GreenProperty, value); }
        }

        static HistogramViewer()
        {
            HistogramViewer.LuminanceProperty = DependencyProperty.Register("Luminance", typeof(PointCollection), typeof(HistogramViewer));
            HistogramViewer.RedProperty = DependencyProperty.Register("Red", typeof(PointCollection), typeof(HistogramViewer));
            HistogramViewer.BlueProperty = DependencyProperty.Register("Blue", typeof(PointCollection), typeof(HistogramViewer));
            HistogramViewer.GreenProperty = DependencyProperty.Register("Green", typeof(PointCollection), typeof(HistogramViewer));
        }

        public HistogramViewer()
        {
            InitializeComponent();
        }

        private PointCollection GetPoints(int[] values)
        {
            if (values == null)
                return null;
            int max = values.Max();
            PointCollection points = new PointCollection();
            points.Add(new Point(0, max));
            for (int i = 0; i < values.Length; i++)
            {
                points.Add(new Point(i, max - values[i]));
            }
            points.Add(new Point(values.Length - 1, max));
            return points;
        }

        public void SetHistogram(Histogram histogram)
        {
            Luminance = GetPoints(histogram.Luminance);
            Red = GetPoints(histogram.Red);
            Blue = GetPoints(histogram.Green);
            Green = GetPoints(histogram.Blue);
        }
    }
}
